package ui.panels;

import entities.User;
import javax.swing.JOptionPane;
import manager.AdminSysDriver;
import manager.GenericDriver;
import ui.windows.RemedySchedulerFrame;

public class SetUserLoginPanel extends javax.swing.JPanel {

    /** Creates new form SetUserLoginPanel */
    public SetUserLoginPanel(RemedySchedulerFrame mainFrame, UserManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        genDriver = mainFrame.getGenericDriver();
        driver = (AdminSysDriver) mainFrame.getDriver();
        initComponents();
        
        lbCheckPass.setText("");
        checkpass = false;
    }

    public void setSelectedUser(User user) {
        this.selectedUser = user;
        if (user != null) {
            lbID.setText(user.getLoginID());
            cbStatus.setSelectedIndex(selectedUser.getStatus() - 1);
            if (user.getLoginPass() != null && user.getLoginPass().length() != 0) {
                lbMess.setText("User's password is available !");
            } else {
                lbMess.setText("User's password is empty !");
            }
        } else {
            lbID.setText("");
            cbStatus.setSelectedIndex(0);
            lbMess.setText("");
        }
    }

    private void setUserLogin() {
        checkPass();
        if(!cbSetPass.isSelected() || checkpass) {
            int status = cbStatus.getSelectedIndex() + 1;
            String pass;
            if(cbSetPass.isSelected()) {
                pass = txtPass.getText();
            } else {
                pass = selectedUser.getLoginPass();
            }
            boolean returnVal = parentPanel.setUserLogin(selectedUser.getUserID(), pass, status);
            if(returnVal) {
                close();
                JOptionPane.showMessageDialog(null, "Updated");
            } else {
                JOptionPane.showMessageDialog(null, "Cannot update", "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    private void setPass() {
        if (cbSetPass.isSelected()) {
            txtPass.setEnabled(true);
            checkPass();
        } else {
            txtPass.setEnabled(false);
            txtPass.setText("");
            lbCheckPass.setText("");
        }
    }

    private void checkPass() {
        if (cbSetPass.isSelected()) {
            String pass = txtPass.getText();
            if (pass != null && pass.length() != 0) {
                lbCheckPass.setText("");
                checkpass = true;
            } else {
                lbCheckPass.setText("enter !");
                checkpass = false;
            }
        }
    }

    private void clear() {
        selectedUser = null;
        lbID.setText("");
        cbStatus.setSelectedIndex(0);
        cbSetPass.setSelected(false);
        txtPass.setText("");
        lbCheckPass.setText("");
        lbMess.setText("");
    }

    private void close() {
        clear();
        parentPanel.closeTarget();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtPass = new javax.swing.JTextField();
        lbID = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbStatus = new javax.swing.JComboBox();
        lbCheckPass = new javax.swing.JLabel();
        cbSetPass = new javax.swing.JCheckBox();
        lbMess = new javax.swing.JLabel();
        bSet = new javax.swing.JButton();
        lbClose = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Set Login", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel1.setText("LoginID:");

        jLabel2.setText("Password:");

        txtPass.setEnabled(false);
        txtPass.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtPassCaretUpdate(evt);
            }
        });

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbID.setText("value");

        jLabel3.setText("Set Status:");

        cbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Open", "Lock" }));

        lbCheckPass.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCheckPass.setForeground(new java.awt.Color(0, 0, 255));
        lbCheckPass.setText("value");

        cbSetPass.setBackground(new java.awt.Color(255, 255, 255));
        cbSetPass.setText("Set password");
        cbSetPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbSetPassActionPerformed(evt);
            }
        });

        lbMess.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbMess.setForeground(new java.awt.Color(0, 0, 255));
        lbMess.setText("value");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbID, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(206, 206, 206))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(cbSetPass)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbMess, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbCheckPass, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSetPass)
                    .addComponent(jLabel2)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckPass))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbMess)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        bSet.setText("Set");
        bSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSetActionPerformed(evt);
            }
        });

        lbClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        lbClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCloseMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bSet, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(lbClose)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbClose)
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bSet)
                .addContainerGap(60, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void lbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbCloseMouseClicked
    close();
}//GEN-LAST:event_lbCloseMouseClicked

private void bSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSetActionPerformed
    setUserLogin();
}//GEN-LAST:event_bSetActionPerformed

private void cbSetPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbSetPassActionPerformed
    setPass();
}//GEN-LAST:event_cbSetPassActionPerformed

private void txtPassCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtPassCaretUpdate
    checkPass();
}//GEN-LAST:event_txtPassCaretUpdate
    private RemedySchedulerFrame mainFrame;
    private UserManagementPanel parentPanel;
    private GenericDriver genDriver;
    private AdminSysDriver driver;
    private User selectedUser;
    private boolean checkpass;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSet;
    private javax.swing.JCheckBox cbSetPass;
    private javax.swing.JComboBox cbStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCheckPass;
    private javax.swing.JLabel lbClose;
    private javax.swing.JLabel lbID;
    private javax.swing.JLabel lbMess;
    private javax.swing.JTextField txtPass;
    // End of variables declaration//GEN-END:variables
}
